package com.yunao.service.report.esou;

import com.yunao.entity.AloneReport;
import com.yunao.entity.Report;
import com.yunao.service.ReportService;
import com.yunao.service.report.info.MailService;
import com.yunao.service.report.info.factory.InfoFactory;
import org.apache.commons.codec.binary.Hex;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 自动报告线程
 * 
 * @author glt
 * 
 */
public class ReportThreadc2 implements Runnable {

	private List<AloneReport> aloneReports;
	private ReportService reportService;
	private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");

	public ReportThreadc2(List<AloneReport> aloneReports,ReportService reportService) {
		this.aloneReports = aloneReports;
		this.reportService = reportService;
	}

	public void run() {
		System.out.println("running..a");
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
	      
		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
		int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
		int dayOfYear = c.get(Calendar.DAY_OF_YEAR);
		int weekOfYear = c.get(Calendar.WEEK_OF_YEAR);
		c.set(2,6);
		int month = c.get(Calendar.MONTH);
		MailService mail = InfoFactory.getMail();
		int i=0;
		System.out.println("start111..."+aloneReports.size());
		for (AloneReport aloneReport : aloneReports) {
			System.out.println("----reproting----"+i++);
		
			// 自动报告方式
			String  manner = aloneReport.getReportmanner();
			System.out.println(manner);
			//String[] addrs = Convert.split(aloneReport.getReportAddrs(), ",");
			String addrs =aloneReport.getReportaddrs();
			// 日报
			if (manner.equals("1") || manner.equals("3") || manner.equals("5") || manner.equals("7")) {
				ReportDaily2 daily = new ReportDaily2(aloneReport,reportService);
				Report report = new Report();
				try {
					String docPath = daily.createMailc();
					String mailtext=daily.createHtmlwordc();
					String docName = aloneReport.getName() + "第" + dayOfYear + "期日报";
					report.setName(docName);
					System.out.println("标题设置成功："+docName);
					report.setFilePath(docPath);
					Map<String, String> attachment = new HashMap<String, String>();
					attachment.put(docName + ".doc", docPath);
					String text = daily.createHtml();

					report.setText(text);

//						String mailtextstr = "<br>如果不希望收到该报告，请点<a href='http://www.yunaodata.com/report_unsubscribe.do?id="+Hex.encodeHexString(String.valueOf(aloneReport.getId()).getBytes())+"&addr="+Hex.encodeHexString(addrs.getBytes())+"'>取消订阅</a><br>";
					String mailtextstr = "/a><br><br>如果不希望收到该报告，可前往后台系统取消订阅<";
					mail.sendMail(docName, new String[]{addrs}, mailtext + mailtextstr, attachment);


					report.setSendResult(1);
					this.log("【" + aloneReport.getName() + "】日报发送完成2");
					
				} catch (Exception e) {
					String docName = aloneReport.getName() + "第" + dayOfYear + "期日报";
					report.setName(docName);
					e.printStackTrace();
					report.setText(report.getText()+e.getMessage());
					report.setSendResult(0);
					this.log("【" + aloneReport.getName() + "】日报发送异常");
				}

				report.setManner(1);

				report.setCreateDate(simpleDateFormat.format(new Date()));
				report.setAddrs(aloneReport.getReportaddrs());
				report.setAloneReportId(aloneReport.getId());
				report.setUserId(aloneReport.getUserId());
				report.setOrganizationId(Integer.parseInt(aloneReport.getSubjectOrganizationId()));
				reportService.save(report);

			}
			// 周报
			if (manner.equals("2") || manner.equals("3") || manner.equals("6") || manner.equals("7")) {
				ReportWeekly2 weekly = new ReportWeekly2(aloneReport,reportService);
				Report report = new Report();
				try {
					System.out.println("---------周报beforedocPath-----------");
					String docPath = weekly.createMailc();
					System.out.println("---------周报afterdocPath-----------");
					String mailtext=weekly.createHtmlwordc();
					System.out.println("---------周报aftermailtext-----------");

					String docName = aloneReport.getName() + "第" + weekOfYear + "期周报";
					report.setName(docName);
					report.setFilePath(docPath);
					Map<String, String> attachment = new HashMap<String, String>();
					attachment.put(docName + ".doc", docPath);
					String text = weekly.createHtml();
					report.setText(text);
//						String mailtextstr = "<br>如果不希望收到该报告，请点<a href='http://www.yunaodata.com/report_unsubscribe.do?id="+Hex.encodeHexString(String.valueOf(aloneReport.getId()).getBytes())+"&addr="+Hex.encodeHexString(addrs.getBytes())+"'>取消订阅</a><br>";
					String mailtextstr = "/a><br><br>如果不希望收到该报告，可前往后台系统取消订阅<";
					mail.sendMail(docName, new String[]{addrs}, mailtext + mailtextstr, attachment);
					report.setSendResult(1);
					this.log("【" + aloneReport.getName() + "】周报发送完成1");
				} catch (Exception e) {
					String docName = aloneReport.getName() + "第" + weekOfYear + "期周报";
					report.setName(docName);
					e.printStackTrace();
					report.setText(report.getText()+e.getMessage());
					report.setSendResult(0);
					this.log("【" + aloneReport.getName() + "】周报发送异常");
				}

				report.setManner(2);
				report.setCreateDate(simpleDateFormat.format(new Date()));
				report.setAddrs(aloneReport.getReportaddrs());
				report.setAloneReportId(aloneReport.getId());
				report.setUserId(aloneReport.getUserId());
				report.setOrganizationId(Integer.parseInt(aloneReport.getSubjectOrganizationId()));
				reportService.save(report);
			}
			// 月报
			if (manner.equals("4") || manner.equals("5") || manner.equals("6") || manner.equals("7")) {
				ReportMonthly2 monthly = new ReportMonthly2(aloneReport,reportService);
				Report report = new Report();
				try {
					System.out.println("---------------月报docPath前-------------------");
					String docPath = monthly.createMailc();
					System.out.println("---------------月报docPath后-------------------");
					String mailtext=monthly.createHtmlwordc();
					System.out.println("---------------月报mailtext后-------------------");

					String docName = aloneReport.getName() + "第" + month + "期月报";
					report.setName(docName);
					report.setFilePath(docPath);
					Map<String, String> attachment = new HashMap<String, String>();
					attachment.put(docName + ".doc", docPath);
					
					String text = monthly.createHtml();
					report.setText(text);
//					String mailtextstr = "<br>如果不希望收到该报告，请点<a href='http://www.yunaodata.com/report_unsubscribe.do?id="+Hex.encodeHexString(String.valueOf(aloneReport.getId()).getBytes())+"&addr="+Hex.encodeHexString(addrs.getBytes())+"'>取消订阅</a><br>";
					String mailtextstr = "/a><br><br>如果不希望收到该报告，可前往后台系统取消订阅<";
					mail.sendMail(docName, new String[]{addrs}, mailtext + mailtextstr, attachment);
					report.setSendResult(1);//发送成功
					this.log("【" + aloneReport.getName() + "】月报发送完成");
				} catch (Exception e) {
					String docName = aloneReport.getName() + "第" + month + "期月报";
					report.setName(docName);
					report.setText(report.getText()+e.getMessage());
					report.setSendResult(0);//发送失败
					this.log("【" + aloneReport.getName() + "】月报发送异常");
				}
				report.setManner(4);
				report.setCreateDate(simpleDateFormat.format(new Date()));
				report.setAddrs(aloneReport.getReportaddrs());
				report.setAloneReportId(aloneReport.getId());
				report.setUserId(aloneReport.getUserId());
				report.setOrganizationId(Integer.parseInt(aloneReport.getSubjectOrganizationId()));
				reportService.save(report);

			}
		}
	}

	public void log(String info) {
		System.out.println(info);
	}

}
